package alg;
import java.util.HashMap;
import java.util.List;

import data.*;
import exception.DataManagerException;

public class DataManagerMySqlCached extends DataManagerMySql {

	final int CACHE_MAX_SIZE = 100000;
	private HashMap<Integer,UserRatingList> cached_ratings;
	private HashMap<Integer,UserRatingList> cached_scores;
	private List<Integer> users;

	public DataManagerMySqlCached() throws DataManagerException {
		super();
		cached_ratings = new HashMap<Integer,UserRatingList>();
		cached_scores = new HashMap<Integer,UserRatingList>();
	}

	@Override
	public UserRatingList getRatings(int UserId) throws DataManagerException {
		if (cached_ratings.containsKey(UserId)) {
			return cached_ratings.get(UserId);

		} else {
			UserRatingList ratings = super.getRatings(UserId);
			cached_ratings.put(UserId,ratings);
			return ratings;
		}
	}

	@Override
	public UserRatingList getScores(int UserId) throws DataManagerException {
		if (cached_scores.containsKey(UserId)) {
			return cached_scores.get(UserId);
			
		} else {
			UserRatingList scores = super.getRatings(UserId);
			cached_scores.put(UserId, scores);
			return scores;
		}
	}

	@Override
	public List<Integer> getAllUsers() throws DataManagerException {
		if (users != null) {
			return this.users;
		} else {
			this.users = super.getAllUsers();
			return this.users;
		}
	}

}
